package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;
import com.gusha.utils.UpdateUtil;

import com.gusha.service.SysAdminService;
import com.gusha.dao.SysAdminDao;
import com.gusha.dao.SysRoleDao;
import com.gusha.model.SysAdmin;
import com.gusha.model.SysRole;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysAdminController控制类
 */
@Controller
@RequestMapping("/gx/sysAdmin")
public class SysAdminController {

    // 注入Dao
    @Autowired
    private SysAdminDao sysAdminDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    
    // 注入Service
    @Autowired
    private SysAdminService sysAdminService;

    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看管理员列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String userName = WebValueUtil.getWebValue(request, "userName");

        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        SysAdmin sysAdmin = new SysAdmin();
        if (StringUtils.isNotBlank(userName)) {
        	sysAdmin.setUserName(userName);
        }

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<SysAdmin> page = sysAdminService.findAll(pageable, sysAdmin);

        model.addAttribute("page", page);
        model.addAttribute("userName", userName);
        return "/admin/sysAdmin/list";
    }

    // 进入添加页面
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入管理员添加页面")
    public String toAdd(Model model) {
    	List<SysRole> roleList = sysRoleDao.findAll();
		model.addAttribute("roleList", roleList);
        return "/admin/sysAdmin/form";
    }

    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入管理员编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");
        List<SysRole> roleList = sysRoleDao.findAll();
		model.addAttribute("roleList", roleList);
        if (StringUtils.isNotBlank(id)) {
            if(sysAdminDao.existsById(new BigInteger(id))) {
                SysAdmin sysAdmin = sysAdminDao.getOne(new BigInteger(id));
                model.addAttribute("obj", sysAdmin);
            }
        }        

        return "/admin/sysAdmin/form";
    }

    // 保存
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存管理员信息")
    public JsonResult save(SysAdmin sysAdmin) {
        JsonResult result = new JsonResult(false,"保存失败");
        if (sysAdmin.getId() != null) {
            SysAdmin sysAdminOld = sysAdminDao.getOne(sysAdmin.getId());
            UpdateUtil.copyNonNullProperties(sysAdmin, sysAdminOld);
        }
        sysAdminDao.saveAndFlush(sysAdmin);
        result.setFlag(true);
        result.setMsg("保存成功");

        return result;
    }


    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除管理员信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (sysAdminDao.existsById(id)) {
            SysAdmin sysAdmin = sysAdminDao.getOne(id);
            sysAdmin.setIsDelete(Constants.DELETE);
            sysAdminDao.saveAndFlush(sysAdmin);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }


    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复管理员信息")
    public JsonResult recovery(BigInteger id) {
        JsonResult result = new JsonResult(false,"恢复失败");
        if (sysAdminDao.existsById(id)) {
            SysAdmin sysAdmin = sysAdminDao.getOne(id);
            sysAdmin.setIsDelete(Constants.RECOVERY);
            sysAdminDao.saveAndFlush(sysAdmin);
            result.setFlag(true);
            result.setMsg("恢复成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }

}
